package com.zyasdk.util;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * �Զ���Dialog
 * 
 * @author Vol
 */
public class Dialog {
	private Context context;

	public Dialog(Context context) {
		this.context = context;
	}

	private android.app.Dialog alertDialog;

	/**
	 * ��ʾģʽ�Ի���
	 * 
	 * @param title
	 * @param message
	 * @param view
	 * @param leftClick
	 * @param rightClick
	 */
	public void show(String title, String message, View view, String lefttext,
			String righttext, final Dialog.OnClickListener leftClick,
			final Dialog.OnClickListener rightClick,
			final OnDismissListener dismiss, final boolean isDismiss,
			boolean isback) {
		try {
			LayoutInflater inflater = LayoutInflater.from(context);
			View mainview = inflater.inflate(
					Util.getLayoutId(context, "zya_dialog_layout"), null);
			LinearLayout layout_mess = (LinearLayout) mainview
					.findViewById(Util.getId(context, "layout_zya_dialog_mess"));

			TextView tv_title = (TextView) mainview.findViewById(Util.getId(
					context, "tv_zya_dialog_title"));
			TextView tv_message = (TextView) mainview.findViewById(Util.getId(
					context, "tv_zya_dialog_message"));
			if (view != null) {
				layout_mess.removeAllViews();
				layout_mess.setPadding(0, 0, 0, 0);
				layout_mess.addView(view);
			} else {
				tv_message.setText(message);
			}
			tv_title.setText(title);
			ImageView iv_dialog_spilt = (ImageView) mainview.findViewById(Util
					.getId(context, "iv_zya_dialog_spilt"));
			Button btn_left = (Button) mainview.findViewById(Util.getId(
					context, "btn_zya_dialog_left"));
			Button btn_right = (Button) mainview.findViewById(Util.getId(
					context, "btn_zya_dialog_right"));
			boolean isspilit = false;
			if ((lefttext == null || lefttext.equals(""))
					&& (righttext == null || righttext.equals(""))) {
				btn_left.setVisibility(View.VISIBLE);
				isspilit = true;
			} else {
				if (lefttext != null && !lefttext.equals("")) {
					btn_left.setText(lefttext);
					btn_left.setVisibility(View.VISIBLE);
					isspilit = true;
				}
				if (righttext != null && !righttext.equals("")) {
					btn_right.setText(righttext);
					btn_right.setVisibility(View.VISIBLE);
				} else {
					isspilit = false;
				}
			}

			if (isspilit) {
				iv_dialog_spilt.setVisibility(View.VISIBLE);
			}

			View.OnClickListener click = new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					if (v.getId() == Util.getId(context, "btn_zya_dialog_left")) {
						if (leftClick != null) {
							leftClick.onClick(Dialog.this, v);
						}
					} else {
						if (rightClick != null) {
							rightClick.onClick(Dialog.this, v);
						}
					}
					if (isDismiss) {
						dismiss();
					}
				}
			};
			btn_left.setOnClickListener(click);
			btn_right.setOnClickListener(click);

			alertDialog = new android.app.Dialog(context, Util.getStyleId(
					context, "zya_dialog_loading"));
			alertDialog
					.setOnDismissListener(new android.app.Dialog.OnDismissListener() {

						@Override
						public void onDismiss(DialogInterface dialog) {
							if (dismiss != null) {
								dismiss.onDismiss();
							}
						}
					});
			alertDialog.setCancelable(isback);
			alertDialog.setContentView(mainview, new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.MATCH_PARENT,
					LinearLayout.LayoutParams.MATCH_PARENT));
			alertDialog.show();
		} catch (Exception e) {
			e.printStackTrace();
			ZYALogUtils.e(e.getMessage());
		}
	}

	public void dismiss() {
		if (alertDialog != null && alertDialog.isShowing()) {
			alertDialog.dismiss();
		}
	}

	public interface OnDatePicker {
		public void onSet(int year, int month, int day);
	}

	public interface OnClickListener {
		public void onClick(Dialog dialog, View view);
	}

	public interface OnDismissListener {
		public void onDismiss();
	}
}
